package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.CustomerReturnList;
import com.atguigu.jxc.entity.CustomerReturnListGoods;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.CustomerReturnListService;
import com.atguigu.jxc.entity.vo.CustomerGoodsVo;
import com.atguigu.jxc.entity.vo.SupplierGoodsVo;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

import java.util.Map;

/**
 * @author qingyu
 * @create 2022-01-02 20:54
 * @description
 */
@RestController
@RequestMapping("/customerReturnListGoods")
public class CustomerReturnListGoodsController {
    @Autowired
    private CustomerReturnListGoodsService customerReturnListGoodsService;
    @Autowired
    private CustomerReturnListService customerReturnListService;
    /**
     * 客户退货单保存 并加库存
     * @param customerReturnList
     * @param customerReturnListGoodsStr
     * @param returnNumber
     * @return
     */
    @PostMapping("/save")
    public String save(CustomerReturnList customerReturnList, String customerReturnListGoodsStr, @RequestParam("returnNumber") String returnNumber){
        customerReturnListGoodsService.save(customerReturnList,customerReturnListGoodsStr,returnNumber);
        Gson gson = new Gson();
        return gson.toJson(new ServiceVO<>(100,"请求成功",null));
    }

    /**
     * 客户退货单查询（可查询条件：退货单号、客户、退款状态）
     * @param returnNumber
     * @param customerId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @PostMapping("/list")
    public String list(String returnNumber, Integer customerId, Integer state, String sTime,String eTime){
        List<CustomerReturnList> customerReturnLists = customerReturnListService.list(returnNumber,customerId,state,sTime,eTime);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",customerReturnLists);
        Gson gson = new Gson();
        return gson.toJson(map);
    }

    /**
     * 通过customerReturnListId退货单商品信息
     * @param customerReturnListId
     * @return
     */
    @PostMapping("/goodsList")
    public String goodsList(Integer customerReturnListId){
        List<CustomerReturnListGoods> customerReturnListGoods = customerReturnListGoodsService.goodList(customerReturnListId);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",customerReturnListGoods);
        Gson gson = new Gson();
        return gson.toJson(map);
    }

    /**
     * 通过customerReturnListId删除退货单
     * @param customerReturnListId
     * @return
     */
    @PostMapping("/delete")
    public String delete(Integer customerReturnListId) {
        customerReturnListGoodsService.delete(customerReturnListId);
        Gson gson = new Gson();
        return gson.toJson(new ServiceVO<CustomerReturnListGoods>(100, "请求成功", null));
    }

    /**
     * 客户退货统计（可根据 商品类别、商品编码或名称 条件查询）
     * @param sTime
     * @param eTime
     * @param goodsTypeId
     * @param codeOrName
     * @return
     */
    @PostMapping("/count")
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){

        List<CustomerGoodsVo> goodsVos = customerReturnListGoodsService.count(sTime,eTime ,goodsTypeId, codeOrName);

        return new Gson().toJson(goodsVos);
    }
}
